package cn.lhf.cd.controller;

import cn.lhf.cd.Main;
import cn.lhf.cd.annotation.FXController;
import cn.lhf.cd.domain.RecordModel;
import cn.lhf.cd.entity.Record;
import cn.lhf.cd.mapper.RecordMapper;
import javafx.collections.FXCollections;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.apache.ibatis.session.SqlSession;

import java.net.URL;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@FXController(path = "/ui/history.fxml")
public class HistoryController extends ChildrenController {
    public Label limitLabel;
    public TableView table;
    public TableColumn dateCol;
    public TableColumn outCol;
    public TableColumn inCol;
    public TableColumn otherCol;

    private boolean isAll = true;
    @Override
    public String getTitle() {
        return "历史数据查询窗口";
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        dateCol.setCellValueFactory(new PropertyValueFactory<>("date"));    //操作时间
        outCol.setCellValueFactory(new PropertyValueFactory<>("out"));      //支出
        inCol.setCellValueFactory(new PropertyValueFactory<>("in"));        //收入
        otherCol.setCellValueFactory(new PropertyValueFactory<>("other"));  //对方帐号
    }

    @Override
    public void bind(MainController mainController, Object... args) {
        super.bind(mainController, args);
        render();
    }

    @Override
    public void onBtnLeft4() throws Exception {
        if (isAll) {
            limitLabel.setText("<<全部数据");
        } else {
            limitLabel.setText("<<近一个月");
        }
        isAll = !isAll;
        render();
    }

    @Override
    public void onBtnRight4() throws Exception {
        getMainController().goHome();
    }

    private void render() {
        List<Record> data = isAll ? getAll() : getAMonths();    //全部 or 近一个月
        ArrayList<RecordModel> models = new ArrayList<>();
        for (Record record:
             data) {
            RecordModel model = new RecordModel();
            model.setDate(record.getTime());
            switch (record.getType()) {
                case WITHDRAWAL:    //取款
                    model.setOut(record.getMoney().toString());
                    model.setIn("0.00");
                    break;
                case PLUS:          //存款
                    model.setIn(record.getMoney().toString());
                    model.setOut("0.00");
                    break;
                case TRANSFER:      //转帐
                    if (getMainController().getSelfId().equals(record.getSrcUserId())) {    //是转出
                        model.setOut(record.getMoney().toString());
                        model.setIn("0.00");
                        model.setOther(record.getDstUserId().toString());
                    } else {        //是转入
                        model.setIn(record.getMoney().toString());
                        model.setOut("0.00");
                        model.setOther(record.getSrcUserId().toString());
                    }
                    break;
            }
            models.add(model);
        }
        table.setItems(FXCollections.observableList(models));
    }

    /**
     * 获取所有流水
     * @return 流水
     */
    private List<Record> getAll() {
        SqlSession sqlSession = Main.openSqlSession();
        List<Record> data = sqlSession.getMapper(RecordMapper.class)
                .getByUserId(getMainController().getSelfId());  //获取所有记录
        sqlSession.close();
        return data;
    }

    /**
     * 获取最近一个月的流水
     * @return 流水
     */
    private List<Record> getAMonths() {
        SqlSession sqlSession = Main.openSqlSession();
        Date dateLimit = Date.valueOf(LocalDate.now().minusMonths(1));  //现在时间减去一个月
        List<Record> data = sqlSession.getMapper(RecordMapper.class)
                .getByUserIdAndDateAfter(getMainController().getSelfId(), dateLimit);   //获取一个月前到今日的所有记录
        sqlSession.close();
        return data;
    }

}
